package com.maycurobj.service.impl;


import com.maycurobj.service.http.CPAPIService;
import com.maycurobj.service.http.HttpAPIService;
import com.maycurobj.util.NormalUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 云枢服务实现类
 */
@Service
public class CPServiceImpl  {

    @Autowired
    private CPAPIService cpapiService;

    @Autowired
    private HttpAPIService httpAPIService;

    /**
     * 废弃
     *
     * 获取地理位置映射map 获取全部系统的城市id，对应关系
     * 返回多重 主键map  crm，oa，两种
     *
     * @param filters
     * @return
     */

    public Map<String,Map<String, JSONObject>> getAreaMap2(JSONArray filters) {

        try {
            String path = String.format("%s%s", this.cpapiService.getURL(), "/api/runtime/query/list");
            String accToken = this.cpapiService.getToken();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("schemaCode", "MKDZK");
            jsonObject.put("mobile", false);
            jsonObject.put("queryCode", "MKDZK");
            jsonObject.put("page", 0);
            jsonObject.put("size", 0);
            jsonObject.put("filters", filters);
            String responseStr = this.httpAPIService.doPost(path, jsonObject.toString(), accToken);
            JSONObject _json = new JSONObject(responseStr);
            JSONObject _data = _json.getJSONObject("data");
            JSONArray _array = _data.getJSONArray("content");

            Map<String, Map<String,JSONObject>> returnMap = new HashMap<>();
            Map<String, JSONObject> map=new HashMap<>();

            for (int i = 0; i < _array.length(); i++) {
                JSONObject _tmp = _array.getJSONObject(i).getJSONObject("data");
                //crm map guid主键
                JSONObject crmJson = new JSONObject();
                if(_tmp.get("crmid")!=null){
                    crmJson.put("u8id", NormalUtil.null2String(_tmp.get("u8id")));
                    crmJson.put("new_area", NormalUtil.null2String(_tmp.get("new_area")));
                    crmJson.put("oaid", NormalUtil.null2String(_tmp.get("oaid")));
                    crmJson.put("wmsid", NormalUtil.null2String(_tmp.get("wmsid")));

                }


            }

            return returnMap;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }


}
